package com.bingxue.edu.teacher.service;

import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.management.education.model.entity.ClassStat;
import com.bingxue.edu.management.education.model.query.ClassQuery;
import com.bingxue.edu.management.education.service.ClassStatService;
import com.bingxue.edu.management.report.model.resp.ClassStatResp;
import com.bingxue.edu.teacher.mapper.TeacherClassStatMapper;
import com.bingxue.edu.teacher.model.query.TeacherClassStatQuery;
import com.bingxue.edu.teacher.model.resp.TeacherClassStatSummaryResp;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.ClassStatTableDef.CLASS_STAT;
import static com.bingxue.edu.management.system.model.entity.table.EmployeeTableDef.EMPLOYEE;
import static com.mybatisflex.core.query.QueryMethods.avg;

@Slf4j
@Service
@RequiredArgsConstructor
public class TeacherClassStatService extends BaseService<TeacherClassStatMapper, ClassStat> {

    private final ClassStatService classStatService;

    public List<ClassStatResp> getClassStatList(TeacherClassStatQuery query) {
        ClassQuery classQuery = new ClassQuery();
        classQuery.setLeadTeacherId(query.getLeadTeacherId());
        classQuery.setClassYear(query.getClassYear());
        classQuery.setTerm(query.getTerm());
        classQuery.setAssistTeacherId(query.getAssistTeacherId());
        return classStatService.getClassStatList(classQuery);
    }

    /**
     * 根据教师 查询教师总体三率
     * @param req 请求信息
     */
    public List<TeacherClassStatSummaryResp> getTeacherClassStatSummary(TeacherClassStatQuery req) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        CLASS_INFO.CLASS_YEAR.as(TeacherClassStatSummaryResp::getClassYear)
                        ,CLASS_INFO.TERM.as(TeacherClassStatSummaryResp::getTerm)
                        ,avg(CLASS_STAT.CANCEL_RATE).as(TeacherClassStatSummaryResp::getCancelRate)
                        ,avg(CLASS_STAT.RENEWAL_RATE).as(TeacherClassStatSummaryResp::getRenewalRate)
                        ,avg(CLASS_STAT.RETAINED_RATE).as(TeacherClassStatSummaryResp::getRetainedRate)
                )
                .from(CLASS_STAT)
                .leftJoin(CLASS_INFO).on(CLASS_STAT.CLASS_ID.eq(CLASS_INFO.ID))
                .leftJoin(EMPLOYEE).on(EMPLOYEE.ID.eq(CLASS_INFO.LEAD_TEACHER_ID))
                .where(CLASS_INFO.LEAD_TEACHER_ID.eq(req.getLeadTeacherId()))
                .and(CLASS_INFO.ASSIST_TEACHER_ID.eq(req.getAssistTeacherId()))
                .and(CLASS_INFO.CLASS_YEAR.eq(req.getClassYear()))
                .and(CLASS_INFO.TERM.eq(req.getTerm()))
                .groupBy(CLASS_INFO.CLASS_YEAR,CLASS_INFO.TERM);
        return this.listAs(queryWrapper, TeacherClassStatSummaryResp.class);
    }

    /**
     * 根据班级id获取统计信息
     * @param id
     * @return
     */
    public ClassStat getClassStatByClassId(String id) {
        return this.queryChain().where(CLASS_STAT.CLASS_ID.eq(id)).one();
    }

}
